﻿using Library.Core.Domain;
using Library.Core.Domain.Specification;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Synthesize.BaseSet.Domain.Specifications.Authority.Role
{

    public class UpdateRoleSpecification : IOperateSpecification<RoleInfoEntity>
    {
        private readonly IRoleRepository _roleRepository;

        public UpdateRoleSpecification(IRoleRepository roleRepository)
        {
            _roleRepository = roleRepository;
        }

        public async Task<bool> SatisfiedBy(RoleInfoEntity entity)
        {

            var roleInfo = await _roleRepository.GetAsync(x => x.Id == entity.Id);
            if (roleInfo == null)
            {
                throw new DomainException("无对应角色信息！");
            }
            if (await _roleRepository.AnyAsync(x => x.Name.Equals(entity.Name) && !x.Id.Equals(entity.Id)))
            {
                throw new DomainException("角色名称重复！");
            }
            return true;
        }
    }
}
